"Colin's method to do ^W= without changing the vertical window size
"
"This seems to work fine, but it is probably not very robust and could
"probably be done more elegantly.
"
"Ideas -
"  A do-while loop would have been very useful.
"  Make a function that could do the typical loop, with j/k and a possible
"     function pointer to what needs to be done at each window.
function! EqualWindowHeightInThisColumn()
   "Save original window number so we can return to it when we are done
   let winNumOrig = winnr()

   call GetToTopWindow()

   "Count of vertical windows to equal-size
   let totalNumVertWindows = 1
   "Cumulative window height (start with height of current window)
   let cumulativeWinHeight = winheight(0)
   "Work down, and for each window, total the height and number of windows
   let winNumStart = winnr()
   wincmd j
   let winNumNew = winnr()
   while (winNumNew != winNumStart)
      let totalNumVertWindows = totalNumVertWindows + 1
      let cumulativeWinHeight = cumulativeWinHeight + winheight(0)

      let winNumStart = winnr()
      wincmd j
      let winNumNew = winnr()
   endwhile
   "echo "numWin:" . totalNumVertWindows . ", cumHeight:" . cumulativeWinHeight

   let heightForEachWin = cumulativeWinHeight / totalNumVertWindows
   let heightForEachWinRemainder = cumulativeWinHeight % totalNumVertWindows
   "echo "heightForEachWin:" . heightForEachWin
   "echo "heightForEachWinRemainder:" . heightForEachWinRemainder

   "Go back to the top (since the resize only seems to work right from
   "top to bottom)
   call GetToTopWindow()

   "Work down again, and for each window, set the height
   execute 'resize ' . heightForEachWin

   let winNumStart = winnr()
   wincmd j
   let winNumNew = winnr()
   while (winNumNew != winNumStart)
      execute 'resize ' . heightForEachWin

      let winNumStart = winnr()
      wincmd j
      let winNumNew = winnr()
   endwhile
   "echo "Done with resize"

   "Not perfect, but try to even out the bottom two windows if remainder height
   if (0 != heightForEachWinRemainder)
      execute 'resize +' . heightForEachWinRemainder / 2
   endif
   "echo "Done with remainder tweak"

   "Work back towards the top until we're where we started
   "  (or at the top, in case something is screwy)
   let winNumStart = winnr()
   if (winNumStart != winNumOrig)
      wincmd k
      let winNumNew = winnr()
      while ((winNumNew != winNumOrig) && (winNumNew != winNumStart))
         let winNumStart = winnr()
         wincmd k
         let winNumNew = winnr()
      endwhile
   endif
   "echo "Done"

endfunction
"Sub-routine for EqualWindowHeightInThisColumn
function! GetToTopWindow()
   "Get to the top window
   let winNumStart = winnr()
   wincmd k
   let winNumNew = winnr()
   while (winNumNew != winNumStart)
      let winNumStart = winnr()
      wincmd k
      let winNumNew = winnr()
   endwhile
endfunction

nmap ,= :call EqualWindowHeightInThisColumn()<CR>

